﻿using Ivony.Html;
using Ivony.Html.Parser;
using RestSharp;
using System.Collections.Generic;
using System.Linq;

namespace bbsScraping
{
    public class BbsScraping
    {
        public void Excute()
        {
            GetCommentList();
        }
        public List<string> GetCommentList()
        {
            var list = new List<string>();
            var TextListUrl = "/forum-news-1449-1.html";
            //IHtmlDocument source = new JumonyParser().LoadDocument(TextListUrl, System.Text.Encoding.GetEncoding("utf-8"));
            RestClient client = new RestClient("http://bbs.cheshi.com/");

            //var request = new RestRequest("/2/short_url/shorten.json?source=2849184197&url_long=" + HttpUtility.UrlEncode(linkurl), Method.GET);
            var request = new RestRequest(TextListUrl, Method.GET);
            IRestResponse response = client.Execute(request);
            var content = response.Content;
            IHtmlDocument source = new JumonyParser().Parse(content);
            //圈子内容
            var sqContent = source.Find(".sq_content");
            if (sqContent == null || sqContent.Count() < 1)
                return list;
            //圈子内容里的文章列表
            var sq_circleList = sqContent.Find(".sq_circleList");
            if (sq_circleList == null || sq_circleList.Count() < 1)
                return list;
            var aDoms = sq_circleList.Find("h3 a");
            if (aDoms == null || aDoms.Count() < 1)
                return list;

            foreach (var a in aDoms)
            {
                var href = a.Attribute("href");
                var strHref = href.AttributeValue;
                if (!string.IsNullOrWhiteSpace(strHref))
                    list.Add(strHref);
            }
            return list;
        }
    }
}
